package com.itheima.controller;

import com.itheima.config.globalExceptionHandle.PasswordException;
import com.itheima.pojo.Emp;
import com.itheima.service.LoginService;
import com.itheima.utils.JwtUtils;
import com.itheima.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Slf4j
@RestController
public class LoginContorller {
    @Autowired
    private LoginService loginService;


    @RequestMapping("/login")
    @PostMapping
    public Result login(@RequestBody Emp emp) throws PasswordException {
//        if (true){
//            throw new PasswordException("用户账号密码错误");
//        }

        log.info("您的用户名与密码{}{}",emp.getUsername(),emp.getPassword());
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",emp.getUsername());
        String s = JwtUtils.generateJwt(map);
        //编写登录流程
        return loginService.loginusername(emp.getUsername(), emp.getPassword());
    }

}
